package com.youyuTech.oneTalkOnline.util;

import java.util.LinkedList;
import java.util.List;

public class WordNode
{

	private int value;

	private List<WordNode> subNodes;

	private boolean isLast;//默认false

	//	public WordNode(int value){
	//		this.value = value;
	//	}

	public WordNode(int value, boolean isLast)
	{
		this.value = value;
		//		if(!this.isLast){//如果已经是某一个敏感词的last，那就是last
		this.isLast = isLast;
		//		}

	}

	/**
	 * 
	 * @param subNode
	 * @return 就是传入的subNode
	 */
	private WordNode addSubNode(final WordNode subNode)
	{
		if (subNodes == null)
			subNodes = new LinkedList<WordNode>();
		subNodes.add(subNode);
		return subNode;
	}

	/**
	 * 有就直接返回该子节点， 没有就创建添加并返回该子节点
	 * @param value
	 * @return
	 */
	public WordNode addIfNoExist(final int value, final boolean isLast)
	{
		if (subNodes == null)
		{
			return addSubNode(new WordNode(value, isLast));
		}
		for (WordNode subNode : subNodes)
		{
			if (subNode.value == value)
			{
				if (!subNode.isLast && isLast)
					subNode.isLast = true;
				return subNode;
			}
		}
		return addSubNode(new WordNode(value, isLast));
	}

	public WordNode querySub(final int value)
	{
		if (subNodes == null)
		{
			return null;
		}
		for (WordNode subNode : subNodes)
		{
			if (subNode.value == value)
				return subNode;
		}
		return null;
	}

	public boolean isLast()
	{
		return isLast;
	}

	public void setLast(boolean isLast)
	{
		this.isLast = isLast;
	}

	public int getValue() {
		return value;
	}

	public void setValue(int value) {
		this.value = value;
	}

	public List<WordNode> getSubNodes() {
		return subNodes;
	}

	public void setSubNodes(List<WordNode> subNodes) {
		this.subNodes = subNodes;
	}

	@Override
	public int hashCode()
	{
		return value;
	}

}
